package cn.topevery.garden.wapi.controller;

import cn.topevery.garden.dal.logic.NoteInfoFileLogic;
import cn.topevery.garden.dal.logic.NoteInfoLogic;
import cn.topevery.garden.entity.dto.BulletinFileDTO;
import cn.topevery.garden.entity.dto.NoteInfoDTO;
import cn.topevery.garden.entity.dto.NoteInfoFileDTO;
import cn.topevery.garden.entity.po.BulletinFile;
import cn.topevery.garden.entity.po.NoteInfoFile;
import cn.topevery.garden.entity.query.BulletinFileQuery;
import cn.topevery.garden.entity.query.NoteInfoFileQuery;
import cn.topevery.garden.entity.query.NoteInfoQuery;
import cn.topevery.garden.entity.vo.BulletinFileVO;
import cn.topevery.garden.entity.vo.NoteInfoFileVO;
import cn.topevery.garden.entity.vo.NoteInfoVO;
import cn.topevery.garden.entity.po.NoteInfo;
import hw.topevery.framework.db.entity.DataPager;
import hw.topevery.framework.util.ExcelUtil;
import hw.topevery.framework.web.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.github.dozermapper.core.Mapper;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.time.LocalDateTime;
import java.util.List;
import java.util.UUID;

/**
 * 操作规范、应急预案、安全教育
 */
@Api(tags = "操作规范、应急预案、安全教育")
@RestController
@RequestMapping("/noteInfo")
public class NoteInfoController extends BaseController {

    @Autowired
    private Mapper mapper;

    @Autowired
    NoteInfoLogic noteInfoLogic;

    @Autowired
    NoteInfoFileLogic noteInfoFileLogic;


    /**
     * 获取操作规范、应急预案、安全教育分页列表
     *
     * @param query
     * @return
     */
    @ApiOperation(value = "获取操作规范、应急预案、安全教育页列表")
    @PostMapping("/listPage")
    public JsonResult<DataPager<NoteInfoVO>> listPage(@RequestBody NoteInfoQuery query) {
        return JsonResult.ok(noteInfoLogic.listPage(query));
    }

    @ApiOperation("新增/编辑 操作规范、应急预案、安全教育信息")
    @PostMapping("/saveOrUpdate")
    public JsonResult<Boolean> saveOrUpdate(@Valid @RequestBody NoteInfoDTO dto) {
        if(ObjectUtils.isEmpty(dto.getId())){
            dto.setMainId(UUID.randomUUID().toString());
            dto.setDate(LocalDateTime.now());
            dto.setUserName(getUser().userName);
        }
        if(ObjectUtils.isEmpty(dto.getOrderNum())){
            dto.setOrderNum(0);
        }
        noteInfoLogic.saveOrUpdate(getUser().id.toString(),mapper.map(dto, NoteInfo.class));

        NoteInfoFileQuery fileQuery = new NoteInfoFileQuery();
        fileQuery.setDataId(dto.getMainId());
        fileQuery.setPageIndex(1);
        fileQuery.setPageSize(100);
        DataPager<NoteInfoFileVO> bulletinFileVODataPager = noteInfoFileLogic.listPage(fileQuery);
        for (NoteInfoFileVO vo : bulletinFileVODataPager.records){
            boolean present = dto.getFileList().stream()
                    .filter(n -> n.getFileId().equals(vo.getFileId()))
                    .findAny().isPresent();
            if(!present){
                NoteInfoFile file = mapper.map(vo, NoteInfoFile.class);
                file.setDbStatus(true); // 删除
                noteInfoFileLogic.saveOrUpdate(getUser().id.toString(),file);
            }
        }

        if(ObjectUtils.isNotEmpty(dto.getFileList())){
            for (NoteInfoFileDTO fileDto : dto.getFileList()){
                boolean present = bulletinFileVODataPager.records.stream()
                        .filter(n -> n.getFileId().equals(fileDto.getFileId()))
                        .findAny().isPresent();
                if(!present) {
                    fileDto.setMainId(dto.getMainId());
                    if (ObjectUtils.isEmpty(fileDto.getDataType())) {
                        fileDto.setDataType((byte) 0);
                    }
                    noteInfoFileLogic.saveOrUpdate(getUser().id.toString(), mapper.map(fileDto, NoteInfoFile.class));
                }
            }
        }
        return JsonResult.ok(true);
    }

    @ApiOperation("通过Id获取详情")
    @GetMapping("/getInfoById")
    public JsonResult<NoteInfoVO> getInfoById(@RequestParam Integer id) {
        NoteInfo entity = noteInfoLogic.getEntity(id);
        NoteInfoVO vo= mapper.map(entity, NoteInfoVO.class);
        return JsonResult.ok(vo);
    }

    @ApiOperation("根据自增ID批量删除")
    @PostMapping("/deleteBatchById")
    public JsonResult<Boolean> deleteBatchById(@RequestBody Integer[] ids) {
        noteInfoLogic.deleteBatchById(getUser().id.toString(),ids);
        return JsonResult.ok(true);
    }

    @ApiOperation("导出操作规范、应急预案、安全教育信息")
    @GetMapping("/export")
    public void export(NoteInfoQuery query, HttpServletResponse response) {
        List<NoteInfoVO> list = noteInfoLogic.listPage(query).records;
        ExcelUtil.exportExcel(list, "操作规范、应急预案、安全教育", "sheet1", NoteInfoVO.class, "操作规范、应急预案、安全教育", response);
    }

}
